<?php

namespace app\admin\controller;

use app\admin\model\Login as LoginModel;

class Login extends Controller
{
    public function login($username,$password){
        $user = LoginModel::get(['Aname' => $username]);
        if (empty($user)){
            return $this->renderError('用户不存在');
        }
        if ($user['Apassword'] == $password) {
            session('Aname', $username);
            return $this->renderSuccess(['token'=>$this->makeToken(),'userinfo'=>$user], '登录成功');
        } else {
            return $this->renderError('用户或密码不正确');
        }
    }

    private function makeToken()
    {
        $str = md5(uniqid(md5(microtime(true)), true)); //生成一个不会重复的字符串
        $str = sha1($str); //加密
        return $str;
    }
}
